﻿using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SweetSurvey.Controllers;
using SweetSurvey.Models;
using SweetSurvey.Models.Repositories;
using SweetSurvey.Models.ViewModels;

namespace SweetSurvey.Tests.ControllerTests
{
    [TestClass]
    public class HomeControllerTest: ControllerTestBase
    {
        [TestMethod]
        public void IndexTest()
        {
            // variables
            var externalUrl = "http://example.com";

            // prepare
            var mockSurveyRepository = new Mock<ISurveyRepository>();
            var mockParticipatorRepository = new Mock<IParticipatorRepository>();
            var controller = new HomeController(mockSurveyRepository.Object, mockParticipatorRepository.Object);
            var mockUrlHelper = new Mock<IUrlHelper>();
            mockUrlHelper.Setup(h => h.IsLocalUrl(externalUrl)).Returns(false);
            controller.Url = mockUrlHelper.Object;

            try
            {
                // act
                controller.Index("http://example.com");

                // assert
                Assert.Fail();
            }
            catch (SecurityException) { }
        }

        [TestMethod]
        public void MySurveysTest()
        {
            // variables
            var surveyCount = 10;

            // prepare
            var surveys = new List<Survey>();
            for (var i = 0; i < surveyCount; i++) { surveys.Add(new Survey()); }
            var mockSurveyRepository = new Mock<ISurveyRepository>();
            mockSurveyRepository.Setup(r => r.GetSurveysByEditorId(DefaultUserName)).Returns(surveys.AsQueryable<Survey>());
            var mockParticipatorRepository = new Mock<IParticipatorRepository>();

            // act
            var controller = new HomeController(mockSurveyRepository.Object, mockParticipatorRepository.Object);
            controller.ControllerContext = GetControllerContext(controller);
            var actionResult = controller.MySurveys();

            // assert
            Assert.IsInstanceOfType(actionResult, typeof(ViewResult));
            Assert.IsInstanceOfType(((ViewResult)actionResult).Model, typeof(IEnumerable<SurveyViewModel>));
            Assert.AreEqual<int>(((IEnumerable<SurveyViewModel>)((ViewResult)actionResult).Model).Count(), surveyCount);
        }

        [TestMethod]
        public void DeleteSurveyTest()
        {
            // variables
            var surveyId = "fakeId";

            // prepare
            var mockSurveyRepository = new Mock<ISurveyRepository>(MockBehavior.Strict);
            mockSurveyRepository.Setup(r => r.AssertOwner(surveyId, this.DefaultUserName));
            mockSurveyRepository.Setup(r => r.Delete(surveyId));
            var mockParticipatorRepository = new Mock<IParticipatorRepository>();

            // act
            var controller = new HomeController(mockSurveyRepository.Object, mockParticipatorRepository.Object);
            controller.ControllerContext = GetControllerContext(controller);
            var actionResult = controller.DeleteSurvey(surveyId);

            // assert
            Assert.IsInstanceOfType(actionResult, typeof(ContentResult));
            Assert.AreEqual<string>(((ContentResult)actionResult).Content, "success");
        }
    }
}
